package com.shrcl.engineerprocess.domain.impl;

import com.google.common.base.Objects;
import com.shrcl.engineerprocess.domain.CorpWeChatUser;
import com.shrcl.engineerprocess.domain.Engineer;
import com.shrcl.engineerprocess.domain.EngineerDAO;
import com.shrcl.engineerprocess.domain.EngineerNotifyReceiver;
import com.shrcl.engineerprocess.domain.EngineerNotitableTaskExtension;
import com.shrcl.engineerprocess.domain.TaskNotification;
import com.shrcl.engineerprocess.domain.TaskNotificationDAO;
import com.shrcl.engineerprocess.domain.impl.TaskNotificationImpl;
import com.shrcl.engineerprocess.domain.proxy.EngineerNotitableTaskExtensionProxy;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCEmployeeUser;
import edu.fudan.langlab.domain.security.User;
import edu.fudan.langlab.domain.task.TSKTask;
import edu.fudan.langlab.domain.task.TSKTaskOwner;
import edu.fudan.langlab.domain.task.TSKUserOwner;
import edu.fudan.langlab.domain.task.impl.TSKTaskExtensionImpl;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("ENGINEERNOTITABLETASKEXTENSION_TYPE")
public abstract class EngineerNotitableTaskExtensionImpl extends TSKTaskExtensionImpl implements EngineerNotitableTaskExtension {
  public EngineerNotitableTaskExtensionImpl() {
    super();
  }
  
  @JoinColumn(name = "taskNotification_id")
  @ManyToOne(targetEntity = TaskNotificationImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private TaskNotification taskNotification;
  
  public TaskNotification getTaskNotification() {
    return this.taskNotification;
  }
  
  public EngineerNotitableTaskExtension setTaskNotification(final TaskNotification taskNotification) {
    this.taskNotification = taskNotification;
    return this;			
    
  }
  
  public Date getNotifyDate() {
    TSKTask _task = this.getTask();
    Date _dueDate = _task.getDueDate();
    return _dueDate;
  }
  
  public Engineer getEngineer() {
    Engineer _xifexpression = null;
    boolean _and = false;
    TSKTaskOwner _owner = this.getOwner();
    if (!(_owner instanceof TSKUserOwner)) {
      _and = false;
    } else {
      TSKTaskOwner _owner_1 = this.getOwner();
      User _user = ((TSKUserOwner) _owner_1).getUser();
      _and = ((_owner instanceof TSKUserOwner) && (_user instanceof RCEmployeeUser));
    }
    if (_and) {
      BeanFactory _beanFactory = this.getBeanFactory();
      EngineerDAO _bean = _beanFactory.<EngineerDAO>getBean(EngineerDAO.class);
      TSKTaskOwner _owner_2 = this.getOwner();
      User _user_1 = ((TSKUserOwner) _owner_2).getUser();
      RCEmployee _employee = ((RCEmployeeUser) _user_1).getEmployee();
      Engineer _asEngineer = _bean.asEngineer(_employee);
      _xifexpression = _asEngineer;
    } else {
      _xifexpression = null;
    }
    return _xifexpression;
  }
  
  public void taskInfoNotify() {
    boolean _and = false;
    Engineer _engineer = this.getEngineer();
    boolean _notEquals = (!Objects.equal(_engineer, null));
    if (!_notEquals) {
      _and = false;
    } else {
      Engineer _engineer_1 = this.getEngineer();
      CorpWeChatUser _asActiveCorpWechatUser = _engineer_1.asActiveCorpWechatUser();
      boolean _notEquals_1 = (!Objects.equal(_asActiveCorpWechatUser, null));
      _and = (_notEquals && _notEquals_1);
    }
    if (_and) {
      Engineer _engineer_2 = this.getEngineer();
      EngineerNotifyReceiver _asNotifyReceiver = _engineer_2.asNotifyReceiver();
      this.taskNotification.setReceiver(_asNotifyReceiver);
      if (this.taskNotification!=null) this.taskNotification.performNotify();
    }
  }
  
  public void createTaskNotification() {
    Engineer _engineer = this.getEngineer();
    boolean _notEquals = (!Objects.equal(_engineer, null));
    if (_notEquals) {
      TSKTask _task = this.getTask();
      Date _dueDate = _task.getDueDate();
      int _year = _dueDate.getYear();
      final int year = (_year + 1900);
      TSKTask _task_1 = this.getTask();
      Date _dueDate_1 = _task_1.getDueDate();
      int _month = _dueDate_1.getMonth();
      final int mon = (_month + 1);
      BeanFactory _beanFactory = this.getBeanFactory();
      final TaskNotificationDAO taskNotificationDAO = _beanFactory.<TaskNotificationDAO>getBean(TaskNotificationDAO.class);
      final Procedure1<TaskNotification> _function = new Procedure1<TaskNotification>() {
          public void apply(final TaskNotification it) {
            TSKTask _task = EngineerNotitableTaskExtensionImpl.this.getTask();
            it.setTskTask(_task);
            it.setTitle("\u4EFB\u52A1\u901A\u77E5");
            Engineer _engineer = EngineerNotitableTaskExtensionImpl.this.getEngineer();
            String _entityName = _engineer.getEntityName();
            String _plus = ("[" + _entityName);
            String _plus_1 = (_plus + "]");
            String _taskName = EngineerNotitableTaskExtensionImpl.this.getTaskName();
            String _plus_2 = (_plus_1 + _taskName);
            String _plus_3 = (_plus_2 + ",\u4EFB\u52A1\u622A\u6B62\u65E5\u671F\u4E3A");
            String _plus_4 = (_plus_3 + Integer.valueOf(year));
            String _plus_5 = (_plus_4 + "\u5E74");
            String _plus_6 = (_plus_5 + Integer.valueOf(mon));
            String _plus_7 = (_plus_6 + "\u6708");
            TSKTask _task_1 = EngineerNotitableTaskExtensionImpl.this.getTask();
            Date _dueDate = _task_1.getDueDate();
            int _date = _dueDate.getDate();
            String _plus_8 = (_plus_7 + Integer.valueOf(_date));
            String _plus_9 = (_plus_8 + "\u65E5");
            it.setBody(_plus_9);
          }
        };
      TaskNotification _create = taskNotificationDAO.create(_function);
      this.taskNotification = _create;
    }
  }
  
  public EngineerNotitableTaskExtensionProxy toProxy() {
    com.shrcl.engineerprocess.domain.proxy.EngineerNotitableTaskExtensionProxy proxy = new com.shrcl.engineerprocess.domain.proxy.EngineerNotitableTaskExtensionProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
